package com.xhtt.smart.card.server.code;

import com.xhtt.smart.card.common.msg.bytes.ServerByteMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;


/**
 * 这是针对 服务器--工卡的 编码，工卡---->服务器 要传设备码，设备类型
 *
 * @author wangze
 * @date 2018-07-03 15:42.
 **/
public class MessageServerEncoder extends MessageToByteEncoder<ServerByteMessage> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, ServerByteMessage msg, ByteBuf byteBuf) throws Exception {
        if (msg == null) {
            return;
        }
        try (ByteBufOutputStream writer = new ByteBufOutputStream(byteBuf)) {
            writer.writeByte(msg.getFrameHeader());
            writer.writeByte(msg.getTransferCode());
            writer.writeByte(msg.getFunctionNumber());
            writer.writeByte(msg.getFunctionCode());
            writer.writeByte(msg.getDataLength());
            //不同类型的数据域是不一样的
            writer.write(msg.getData());
            writer.write(msg.getCheckNumber());
            writer.write(msg.getFrameFoot());

        }

    }
}
